<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 菜品口味关系管理
 *
 * @icon fa fa-circle-o
 */
class DishFlavor extends Backend
{

    /**
     * DishFlavor模型对象
     * @var \app\admin\model\DishFlavor
     */
    protected $model = null;
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\DishFlavor;
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['dish'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * Selectpage的实现方法
     *
     * 当前方法只是一个比较通用的搜索匹配,请按需重载此方法来编写自己的搜索逻辑,$where按自己的需求写即可
     * 这里示例了所有的参数，所以比较复杂，实现上自己实现只需简单的几行即可
     *
     */
    public function selectpage()
    {
        $list = [];
        //自定义搜索条件
        $custom = (array)$this->request->request("custom/a");
        // 获取菜品口味类型列表
        $dishFlavorTypeList =  $this->model->getDishFlavorTypeList();
        // dump($dishFlavorTypeList);
        if (empty($custom)) {
            if (!empty($dishFlavorTypeList)) {
                $keys = array_keys($dishFlavorTypeList);
                foreach ($keys as $key => $value) {
                    $list[] = [
                        'id' => $key,
                        'name' => $value
                    ];
                }
            }
        } else {
            $index = isset($custom['index']) ? $custom['index'] : '';
            if ($index !== '') {
                if (!empty($dishFlavorTypeList)) {
                    $keys = array_keys($dishFlavorTypeList);
                    foreach ($keys as $key => $value) {
                        $list[] = [
                            'id' => $key,
                            'name' => $value
                        ];
                    }
                    if ($index != 0) {
                        $tmp = $list[0];
                        $list[0] =  $list[$index];
                        $list[$index] =  $tmp;
                    }
                }
            }
            $key = isset($custom['key']) ?  $custom['key'] : '';
            if (!empty($key)) {
                $val = $dishFlavorTypeList[$key];
                $arr = explode(',', $val);
                foreach ($arr as $k => $v) {
                    $list[] = [
                        'id' => $k,
                        'name' => $v
                    ];
                }
            }
        }
        //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
        return json(['list' => $list]);
    }
}
